<?php

namespace backend\modules\Product\widgets;

use backend\models\c2\form\ProductPackageForm;
use backend\models\c2\form\SkuAssembleForm;
use backend\models\c2\form\EavForm;
use backend\models\c2\form\EavSkuForm;
use common\models\c2\entity\ProductStock;
use common\models\c2\search\ProductPackageSearch;
use kzo\base\widgets\ui\common\part\EntityDetail as DetailWidget;
use Yii;

class EntityDetail extends DetailWidget
{
    public $withTranslationTabs = false;

    public $withProfileTab = false;
    public $withEavTab = false;
    public $withSkuTab = false;
    public $withPackageTab = false;
    public $withStockTab = false;

    public function getTabItems()
    {
        $items = [];

        if ($this->withStockTab) {
            $items[] = $this->getStockTab();
        }

        if ($this->withPackageTab) {
            $items[] = $this->getPackageTab();
        }

        if ($this->withSkuTab) {
            $items[] = $this->getSkuTab();
        }

        if ($this->withEavTab) {
            $items[] = $this->getEavTab();
        }

        if ($this->withBaseInfoTab) {
            $_form = '_form';
            if ($this->model->hasProperty('formView')) {
                $_form = $this->model->formView;
            }
            $items[] = [
                'label' => Yii::t('app.c2', 'Base Information'),
                'content' => $this->controller->renderPartial($_form, ['model' => $this->model,]),
                'active' => true,
            ];
        }
        $items[] = [
            'label' => '<i class="fa fa-feather"></i> ' . $this->tabTitle,
            'onlyLabel' => true,
            'headerOptions' => [
                'class' => 'pull-left header',
            ],
        ];
        return $items;
    }

    public function getSkuTab()
    {
        if (!isset($this->_tabs['SKU_TAB'])) {
            if (!$this->model->isNewRecord) {
                $this->_tabs['SKU_TAB'] = [
                    'label' => Yii::t('app.c2', 'Product Sku Management'),
                    'content' => $this->controller->renderPartial('/default/_sku_form', ['model' => new EavSkuForm(['entityModel' => $this->model])]),
                    'enable' => true,
                ];
            } else {
                $this->_tabs['SKU_TAB'] = [
                    'label' => Yii::t('app.c2', 'Product Sku Management'),
                    'content' => "",
                    'enable' => false,
                ];
            }
        }
        return $this->_tabs['SKU_TAB'];
    }

    public function getEavTab()
    {
        if (!isset($this->_tabs['EAV_TAB'])) {
            if (!$this->model->isNewRecord) {
                $this->_tabs['EAV_TAB'] = [
                    'label' => Yii::t('app.c2', 'Eav Attributes'),
                    'content' => $this->controller->renderPartial('/default/_eav_form', ['model' => new EavForm(['entityModel' => $this->model])]),
                    'enable' => true,
                ];
            } else {
                $this->_tabs['EAV_TAB'] = [
                    'label' => Yii::t('app.c2', 'Eav Attributes'),
                    'content' => "",
                    'enable' => false,
                ];
            }
        }

        return $this->_tabs['EAV_TAB'];
    }

    public function getPackageTab()
    {
        if (!isset($this->_tabs['PACKAGE_TAG'])) {
            if (!$this->model->isNewRecord) {
                // $model = new ProductPackageForm(['entityModel' => $this->model]);
                // $model->loadItems();
                $searchModel = new ProductPackageSearch();
                $searchModel->product_id = $this->model->id;
                $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
                $this->_tabs['PACKAGE_TAG'] = [
                    'label' => Yii::t('app.c2', 'Package'),
                    'content' => $this->controller->renderPartial('/package/index', [
                        'model' => $this->model,
                        'searchModel' => $searchModel,
                        'dataProvider' => $dataProvider,
                    ]),
                    'enable' => true,
                ];
            } else {
                $this->_tabs['PACKAGE_TAG'] = [
                    'label' => Yii::t('app.c2', 'Package'),
                    'content' => "",
                    'enable' => false,
                ];
            }
        }
        return $this->_tabs['PACKAGE_TAG'];
    }

    public function getStockTab()
    {
        if (!isset($this->_tabs['STOCK_TAG'])) {
            if (!$this->model->isNewRecord) {
                $model = ProductStock::findOne(['product_id' => $this->model->id]);
                if (is_null($model)) {
                    $model = new ProductStock();
                }
                $model->product_id = $this->model->id;
                $this->_tabs['STOCK_TAG'] = [
                    'label' => Yii::t('app.c2', 'Stock'),
                    'content' => $this->controller->renderPartial('/default/_stock_form', ['model' => $model]),
                    'enable' => true,
                ];
            } else {
                $this->_tabs['STOCK_TAG'] = [
                    'label' => Yii::t('app.c2', 'Stock'),
                    'content' => "",
                    'enable' => false,
                ];
            }
        }
        return $this->_tabs['STOCK_TAG'];
    }

}